/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities'),
		id = sessionStorage.id;

	var app = new Vue({
		el: '#uc_mytags',
		data: {
			list: {},
			types: [/*{
				code: 773,
				name: 'motion',
				zhName: '运动',
				checkedList: []
			}, {
				code: 806,
				name: 'food',
				zhName: '食物',
				checkedList: []
			}, {
				code: 821,
				name: 'book',
				zhName: '书籍',
				checkedList: []
			}, {
				code: 843,
				name: 'movie',
				zhName: '电影',
				checkedList: []
			}, {
				code: 860,
				name: 'hobby',
				zhName: '业余爱好',
				checkedList: []
			}, {
				code: 881,
				name: 'tourism',
				zhName: '旅游',
				checkedList: []
			}, {
				code: 895,
				name: 'program',
				zhName: '关注的节目',
				checkedList: []
			}, {
				code: 908,
				name: 'amuse',
				zhName: '娱乐休闲',
				checkedList: []
			}, */{
				code: 929,
				name: 'label',
				zhName: '个人标签',
				checkedList: []
			}],
			checkedList: [],
			totalChecked: 0
		},
		methods: {
			init: function () {
				app.detail = utilities.base64TojsonObj(sessionStorage.userInfo).detail;

				var req = '';
				for (var i = 0; i < app.types.length; i++) {
					req += (app.types[i].code + ',');
				}
				_(Config.DICTIONARY, {
					parentId: req
				}, function (data) {
					var tagsList = data.obj;
					for (var i = 0; i < app.types.length; i++) {
						var type = app.types[i];
						for (var j = 0; j < data.obj[type.code].length; j++) {
							var list = data.obj[type.code];
							var checked = app.detail[utilities.findElement(app.types, 'code', type.code).name].split(',');
							for (var k = 0; k < checked.length; k++) {
								if ((list[j].id == checked[k]) && checked[k] != '') {
									type.checkedList.push(list[j].id);
								}
							}
						}
					}
					app.list = tagsList;
					app.calcTotalChecked();
				});
			},
			save: function () {
				var req = {};
				for (var i = 0; i < app.types.length; i++) {
					var str = '';
					for (var j = 0; j < app.types[i].checkedList.length; j++) {
						str += (app.types[i].checkedList[j] + ',');
					}
					if (str !== '') {
						req['xgdetail.' + app.types[i].name] = str.substr(0, str.length - 1);
					}
				}
				req.id = id;
				_(Config.SERVICE.USERCENTER.TAG_SAVE, req, function (data) {
					if (data.code == 200 && data.type === 'SUCCESS') {
						alert('保存成功！');
						utilities.refreshUserInfo(function (data) {
							history.back();
						});
					}
				});
			},
			isChecked: function (id, typeId) {
				var type = app.findElement(app.types, 'code', typeId);
				for (var i = 0; i < type.checkedList.length; i++) {
					if (type.checkedList[i] === id) {
						return true;
					}
				}
			},
			pick: function (id, typeId) {
				var type = app.findElement(app.types, 'code', typeId);
				for (var i = 0; i < type.checkedList.length; i++) {
					if (id === type.checkedList[i]) {
						this.removeValue(type.checkedList, id);
						app.calcTotalChecked();
						return;
					}
				}
				type.checkedList.push(id);
				app.calcTotalChecked();
			},
			calcTotalChecked: function () {
				var num = 0;
				for (var i = 0; i < app.types.length; i++) {
					num += app.types[i].checkedList.length;
				}
				app.totalChecked = num;
			},
			removeValue: function (arr) {
				var what, a = arguments, L = a.length, ax;
				while (L > 1 && arr.length) {
					what = a[--L];
					while ((ax = arr.indexOf(what)) !== -1) {
						arr.splice(ax, 1);
					}
				}
				return arr;
			},
			findElement: utilities.findElement
		}

	});

	app.init();
});
